<template>
	<view>
		<view class="header">
			<x-status-bar></x-status-bar>
			<view class="flex-center-between header-navbar">
				<x-back-btn></x-back-btn>
				<view class="common-title">Paypal</view>
			</view>
		</view>
		
		<web-view :src="gmapSrc" @message="getMessage"></web-view>
	</view>
</template>

<script>
	import { TOKENNAME, APP_LANG } from '@/config.js'
	export default {
		data() {
			return {
				webview: null,
				gmapSrc: '',
				orderObj: {},
				event: null,
				
				status: false
			}
		},
		onLoad() {
			// 获取全局事件
			this.event = this.getOpenerEventChannel();
			this.event.on('paypal-page', (orderNumber) => {
				console.log('orderNumber => ', orderNumber)
				this.orderNumber = orderNumber
				this.gmapSrc = this.changeLang()
				this.changeWebViewBottom()
			})
		},
		// onBackPress() {
		// 	this.event.emit('change', this.status)
		// },
		methods: {
			// 切换语言
			changeLang() {
				let str = `/static/pages/paypal/index.html?orderNumber=${ this.orderNumber }&token=${ uni.getStorageSync(TOKENNAME) }`
				return str 
			},
			// 响应数据
			changeWebViewBottom() {
				// 获取当前屏幕属性
				const sysInfo = uni.getSystemInfoSync()
				const { screenHeight, statusBarHeight, windowBottom } = sysInfo
				
				setTimeout(() => {
					let top = statusBarHeight + uni.upx2px(98)
					var currentWebview = this.$scope.$getAppWebview()
					this.webview = currentWebview.children()[0]
					this.webview.setStyle({
						height: (screenHeight - top) + 'px',
						top: top + 'px',
						bottom: '0px'
					})
					this.webview.show()
				}, 300)
			},
			getMessage(e) {
				// 解析H5回传的消息
				let { value, status } = e.detail.data[0] || {}
				// 返回两个字段，value表示订单返回的回执数据，status表示是否成功请求
				this.$store.dispatch('initUserData')
				this.status = status
				this.jumpBack()
			}
		}
	}
</script>

<style>

</style>